﻿Imports System.Data.SqlClient

Public Class frmEmploymentLetter
    Private Sub frmIncidentsReports_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        fillSigners()
        Me.WindowState = FormWindowState.Maximized
    End Sub

    Sub fillSigners()
        Try
            Dim dat As New SqlDataAdapter("Select FullNameAr,dbo.GetArabicPosition(Position) as Position from StaffProfiles", cnn)
            Dim dt As New DataTable
            dat.Fill(dt)
            CombSignedBy.DataSource = Nothing
            CombSignedBy.DataSource = dt
            CombSignedBy.DisplayMember = "FullNameAr"
            CombSignedBy.ValueMember = "Position"
            CombSignedBy.SelectedIndex = -1
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
        End Try
    End Sub
    Private Sub txtId_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Clear()
    End Sub

    Sub Clear()
        Me.TxtName.Clear()
        Me.txtBranch.Clear()
        Me.txtPosition.Clear()
    End Sub

    Sub FillEmpData()
        If Me.txtEmpNo.Text.Trim = "" Then
            Exit Sub
        Else

            Try
                Me.Cursor = Cursors.WaitCursor
                Dim cmd As New SqlCommand("Select FullNameAr,dbo.GetArabicPosition(Position)" & _
                " as Position, dbo.GetArabicBranch(Branch) as Branch From StaffProfiles where EmpNo= " & Me.txtEmpNo.Text & "", cnn)
                Dim Reader As SqlDataReader
                Me.TxtName.Clear()
                Me.txtPosition.Clear()
                Me.txtBranch.Clear()
                cnn.Open()
                Reader = cmd.ExecuteReader
                While Reader.Read
                    Me.TxtName.Text = Reader.Item("FullNameAr")
                    Me.txtPosition.Text = Reader.Item("Position")
                    Me.txtBranch.Text = Reader.Item("Branch")

                End While
                cnn.Close()
                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub txtEmpNo_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtEmpNo.KeyUp
        If e.KeyCode = Keys.Enter Then
            FillEmpData()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As New frmSearchEmp
        a.ShowDialog()
        If SelEmpID = 0 Then
            Exit Sub
        End If
        Me.txtEmpNo.Text = SelEmpID
        FillEmpData()
    End Sub

    Private Sub butClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Sub settxt(ByRef _rpt As Object, ByVal txtName As String, ByVal txt As String, Optional ByVal Supress As Boolean = False)

        Dim myTextObjectOnReport As CrystalDecisions.CrystalReports.Engine.TextObject
        If TypeOf _rpt.ReportDefinition.ReportObjects.Item(txtName) Is CrystalDecisions.CrystalReports.Engine.TextObject Then
            myTextObjectOnReport = _rpt.ReportDefinition.ReportObjects.Item(txtName)
            myTextObjectOnReport.Text = txt
            myTextObjectOnReport.ObjectFormat.EnableSuppress = Supress
        End If

    End Sub

    Sub PrintCertificate()
        If TxtName.Text.Trim.Length = 0 Then
            MsgBox("Please enter a valid staff ID!")
            Exit Sub
        Else
            Try
                Me.Cursor = Cursors.WaitCursor
                'saving the issuance of the letter in the archive
               

                Dim cmd As New SqlCommand("insert into LettersArchive (EmpName , EmpID , LetterType , IssuedBy,SignedBy) values (@EmpName , @EmpID , @LetterType , @IssuedBy,@SignedBy)", cnn)
                cmd.Parameters.AddWithValue("@EmpName", Me.TxtName.Text.Trim)
                cmd.Parameters.AddWithValue("@EmpID", CInt(Me.txtEmpNo.Text.Trim))
                cmd.Parameters.AddWithValue("@LetterType", "خطاب تعريف")
                cmd.Parameters.AddWithValue("@IssuedBy", CurrentUser)
                cmd.Parameters.AddWithValue("@SignedBy", Me.CombSignedBy.Text)
                cnn.Open()
                cmd.ExecuteNonQuery()
                cnn.Close()
                'issuing the letter
                Dim das As New DataTable
                Dim dap As New SqlDataAdapter("SELECT EmpNo, Gender, FullNameAr, FullNameEn, dbo.GetArabicNationality(Nationality) as Nationality, " & _
               "Email, Mobile, DateofBirth, Address, dbo.GetArabicPosition(Position) as Position, HireDate" & _
               ", VacationPeriod, Branch, IDNo, IDExDate, IDIssuePlace, IDIssuePlaceAr, IDIssueDate," & _
               " PassportNo, ExDate, PassIssuePlace, PassIssuePlaceAr, PassIssueDate, ReligionEn," & _
               " SalaryBS, SalaryHA, SalaryTA, TotalSalary, SalaryMobile, Photo, Others, ResidenceNo" & _
               ", EndOfServiceDate, HireYears, EndOfServiceReason, EndOfServiceBenefits,EndOfServiceBenefitsWritten, Comment," & _
               " Active FROM StaffProfiles Where (EmpNo=@EmpNo)", cnn)
                dap.SelectCommand.Parameters.AddWithValue("@EmpNo", Me.txtEmpNo.Text)

                dap.Fill(das)

                ' Creat Report and Pass The data

                Dim rpt As New rptEmploymentLetter
                rpt.SetDataSource(das)
                Dim Signer, SignerPosition As CrystalDecisions.CrystalReports.Engine.TextObject

                Signer = rpt.ReportDefinition.Sections(0).ReportObjects("txtSigner")
                SignerPosition = rpt.ReportDefinition.Sections(0).ReportObjects("txtSignerPosition")

                Signer.Text = CombSignedBy.Text
                SignerPosition.Text = CombSignedBy.SelectedValue
                    Me.CrystalReportViewer1.ReportSource = rpt
                    Me.CrystalReportViewer1.RefreshReport()


                txtEmpNo.Focus()
                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub txtEmpNo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEmpNo.TextChanged
        Clear()
    End Sub

    Private Sub butGenerat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butGenerat.Click
        If Me.txtEmpNo.Text.Trim.Length = 0 Or IsNumeric(Me.txtEmpNo.Text) = False Then
            MsgBox("رجاءا اختر الموظف")
            txtEmpNo.Clear()
        ElseIf CombSignedBy.SelectedIndex = -1 Then
            MsgBox("رجاءا اختر الموظف الموقع على الخطاب")
        Else
            PrintCertificate()
        End If
    End Sub
End Class